package com.example.common.manager.web;

import com.example.common.config.QuestionConfig;
import com.example.common.manager.domain.StudentMsg;
import com.example.common.manager.server.DrawQuestionsServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;


/**
 * 抽题控制类
 */
@RestController
public class DrawQuestionsController {

    @Autowired
    DrawQuestionsServer drawQuestionsServer;

    @GetMapping("/drawQuestion")
    public void drawQuestions(String classNumber) {

        // 根据班级随机抽取 学生信息
        StudentMsg student = drawQuestionsServer.getStudent(classNumber);
        System.out.println("幸运同学是: " + student.getStudentName() +
                " 目前答题次数: " + student.getCount() + " 目前分数: "
                + student.getScore());

        // 换行
        System.out.println();

        question();

        // 根据答对的题目进行加分
        BigDecimal b1 = new BigDecimal(student.getScore());

        System.out.println("请输入获取的分数: ");
        Scanner scanner = new Scanner(System.in);
        String score = scanner.next();
        BigDecimal b2 = new BigDecimal(score);
        b1 = b1.add(b2);
        System.out.println("目前 " + student.getStudentName() + " 同学的总分数是: " + b1 +" 继续加油");

        student.setScore(Integer.valueOf(String.valueOf(b1)));
        student.setCount(student.getCount() + 1);

        //  是否选择继续挑战【可以选择消耗一分 继续抽题】
        System.out.println("请选择是否继续挑战: ");
        String ch = scanner.next();
        if (!ch.equals("N")){
            // 抽题
            question();
            // 计算分数和答题次数
            System.out.println("请输入获取的分数: ");
            scanner = new Scanner(System.in);
            score = scanner.next();
            student.setScore(student.getScore() + Integer.parseInt(score) -1);
            student.setCount(student.getCount() + 1);
        }

        //  用原来的分数加上 现在的分数 再将此分数更新到数据库
        // 更新分数 和 答题次数
        if (classNumber.equals("3")){
            drawQuestionsServer.updateScoreThree(student);
        }else {
            drawQuestionsServer.updateScore(student);
        }

    }

    public void question() {
        // 随机抽取2道题目
        List<String> questions = QuestionConfig.getQuestions();
        // 打乱集合顺序 截取前二
        Collections.shuffle(questions);
        List<String> strings = questions.subList(0, 2);
        //输出题目
        for (String string : strings) {
            System.out.println(string);
        }
    }
}
